package com.example.myapplication.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;
import com.example.myapplication.database.AccountBean;
import com.example.myapplication.database.DBManager;
import com.example.myapplication.ui.adapter.AccountAdapter;

import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends AppCompatActivity {

    private ImageView searchIvBack;
    private EditText searchEt;
    private ImageView searchIvSh;
    private ListView searchLv;
    private TextView searchTv;
    List<AccountBean>mDatas;//数据源
    AccountAdapter adapter;//适配器对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initView();
        mDatas=new ArrayList<>();
        adapter=new AccountAdapter(this,mDatas);
        searchLv.setAdapter(adapter);
        searchLv.setEmptyView(searchTv);//设置无数据时，显示的布局
    }

    /**
     * 初始化控件
     */
    private void initView() {
        searchIvBack = (ImageView) findViewById(R.id.search_iv_back);
        searchEt = (EditText) findViewById(R.id.search_et);
        searchIvSh = (ImageView) findViewById(R.id.search_iv_sh);
        searchLv = (ListView) findViewById(R.id.search_lv);
        searchTv = (TextView) findViewById(R.id.search_tv);
    }
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.search_iv_back:
                finish();
                break;
            case R.id.search_iv_sh:   //开始执行搜索操作
                String text = searchEt.getText().toString();
//                判断输入内容是否为空，如果为空，提示不能搜索
                if (TextUtils.isEmpty(text)) {
                    Toast.makeText(this,"输入内容不能为空！",Toast.LENGTH_SHORT).show();
                    return;
                }
                //开始搜索
                List<AccountBean> list = DBManager.searchInfoByAccount(text);
                mDatas.clear();
                mDatas.addAll(list);
                adapter.notifyDataSetChanged();
                break;
        }
    }
}